<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>插入排序</title>
</head>
<body>
    
    <script>
    
        {
            // 插入排序

            let ary = [10,15,13,12,8,14,9,11];

            function insertSort (ary) {
                let temp = [];
                temp.push(ary[0]);
                for (let i=1;i<ary.length;i++) {
                    let ele = ary[i];
                    for(let j=temp.length-1;j>=0;j--){
                        let tempEle = temp[j];
                        if(ele>tempEle){
                            temp.splice(j+1,0,ele);
                            break;
                        }
                        if(j===0){
                            temp.unshift(ele);
                        }
                    }
                }
                return temp;
            }

            console.log(insertSort(ary));
        }

    </script>

</body>
</html>